/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tp_imag.gui.area.selection;

import ij.ImagePlus;
import ij.gui.PolygonRoi;
import ij.io.FileInfo;
import ij.io.FileOpener;
import ij.io.Opener;
import ij.process.ColorProcessor;

import java.awt.BorderLayout;
import java.awt.image.BufferedImage;

import org.jdesktop.application.Action;

import tp_imag.gui.components.JImagePanel;
import tp_imag.gui.selection.SelectionPanel;
import tp_imag.model.ActualImage;


/**
 *
 * @author LRinaudo
 */
public class AreaSelectionPanel extends javax.swing.JPanel{

    private BufferedImage loadImg;
    private ImagePlus newImage;
    private SelectionPanel selPanel = new SelectionPanel();
    Mouse_Listener mouseListener = new Mouse_Listener();
    private static String NEW_IMAGE_PATH = "areaSelection.jpg";

    /** Creates new customizer AreaSelectionPanel */
    public AreaSelectionPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the FormEditor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jOriginalPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();

        jButton1.setText("jButton1");

        setLayout(new java.awt.GridLayout(1, 0));

        jOriginalPanel.setPreferredSize(new java.awt.Dimension(500, 325));
        jPanel2.add(jOriginalPanel);

        jButton2.setLabel("Seleccionar sector");
        jButton2.setPreferredSize(new java.awt.Dimension(120, 25));
        jPanel1.add(jButton2);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	selectAreaAction(evt);
            }
        });

        jPanel2.add(jPanel1);

        add(jPanel2);
    }// </editor-fold>//GEN-END:initComponents


    private void loadImage() {
        try {
            loadImg = ActualImage.getInstance().getOriginal();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    private void showOriginalImg() {

        try {
            //BufferedImage loadImg = ImageIO.read(new File("Perfil3.jpg"));
            jOriginalPanel.setLocation(1, 1);
            int h = loadImg.getHeight();
            int w = loadImg.getWidth();

            jOriginalPanel.setSize(w, h);
            JImagePanel panel = new JImagePanel(loadImg, 0, 0, jOriginalPanel.getSize());

            jOriginalPanel.setLayout(new BorderLayout());
            panel.setVisible(true);
            jOriginalPanel.add(panel);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public void refreshScreen() {
        
    	loadImage();
        showOriginalImg();
    }
    
    public ImagePlus getNewImage (){
    	Opener fileOpener = new Opener();
    	this.newImage = fileOpener.openImage(NEW_IMAGE_PATH);
    	return this.newImage;
    }

    @Action
    public void selectAreaAction(java.awt.event.ActionEvent evt) {
    	int x, y;
    	ImagePlus imagePlus = new ImagePlus();
    	imagePlus.setImage(loadImg);
    	ColorProcessor imageProcessor = new ColorProcessor(loadImg);
    	/*AreaSelected areaSelected = new AreaSelected();
    	areaSelected.setup(TOOL_TIP_TEXT_KEY, imagePlus);
    	areaSelected.run(imageProcessor);*/
        mouseListener.setup(TOOL_TIP_TEXT_KEY, imagePlus, jOriginalPanel);
        mouseListener.run(imageProcessor);
        this.newImage = mouseListener.getImage();
        
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jOriginalPanel;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    // End of variables declaration//GEN-END:variables

}
